package com.jp.knowledge.activity;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.JsonObject;
import com.jiayen.util.ToasUtil;
import com.jp.knowledge.R;
import com.jp.knowledge.comm.SlidingActivity;
import com.jp.knowledge.dialog.SelectDialog;
import com.jp.knowledge.http.HttpHelper;
import com.jp.knowledge.model.IModel;
import com.jp.knowledge.my.activity.UserSettingEditActivity;
import com.jp.knowledge.util.ResultSubscriber;

import org.xutils.view.annotation.ViewInject;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Locale;

/**
 * Created by 528787067@qq.com on 2017/7/27.
 */

public class AdjustSortActivity extends SlidingActivity implements View.OnClickListener, ResultSubscriber.OnResultListener {

	protected static final int SELECT_SORT_TYPE = 0x01;
	protected static final int UPDATE_BY_ADMIN_CODE = 0x02;
	private static final int SPIDER_CODE = 0x03;

	protected String id;
	private String navtype;
	private String navtype2;

	private SelectDialog selectDialog;
	private DateFormat dateFormat;

	@ViewInject(R.id.right_bn1)
	protected TextView collectionBtn;

	@ViewInject(R.id.item_type)
	private LinearLayout itemType;
	@ViewInject(R.id.item_title)
	private LinearLayout itemTitle;
	@ViewInject(R.id.item_choice)
	protected LinearLayout itemChoice;
	@ViewInject(R.id.item_open)
	private LinearLayout itemOpen;
	@ViewInject(R.id.item_time)
	protected LinearLayout itemTime;

	@ViewInject(R.id.item_type_desc)
	protected TextView itemTypeDesc;
	@ViewInject(R.id.item_title_desc)
	protected TextView itemTitleDesc;
	@ViewInject(R.id.item_choice_desc)
	private TextView itemChoiceDesc;
	@ViewInject(R.id.item_open_desc)
	protected TextView itemOpenDesc;
	@ViewInject(R.id.item_time_desc)
	private TextView itemTimeDesc;

	@ViewInject(R.id.collection_view)
	protected LinearLayout collectionView;
	@ViewInject(R.id.item_website)
	private LinearLayout itemWebsite;
	@ViewInject(R.id.item_headline)
	private LinearLayout itemHeadline;
	@ViewInject(R.id.item_website_desc)
	private TextView itemWebsiteDesc;
	@ViewInject(R.id.item_headline_desc)
	private TextView itemHeadlineDesc;

	@Override
	protected int getResourceId() {
		return R.layout.activity_adjust_sort;
	}

	@Override
	protected void init() {
		id = getIntent().getStringExtra("id");
		dateFormat = new SimpleDateFormat("yyyyMMddhhmmss", Locale.getDefault());
		initTitleBar();
		itemChoiceDesc.setTag(false);
		itemOpenDesc.setTag(true);
		itemHeadlineDesc.setTag(true);
		itemChoiceDesc.setText("否");
		itemOpenDesc.setText("是");
		itemHeadlineDesc.setText("是");
		itemTimeDesc.setText(dateFormat.format(System.currentTimeMillis()));
		itemType.setOnClickListener(this);
		itemTitle.setOnClickListener(this);
		itemChoice.setOnClickListener(this);
		itemOpen.setOnClickListener(this);
		itemTime.setOnClickListener(this);
		itemWebsite.setOnClickListener(this);
		itemHeadline.setOnClickListener(this);
		if(getIntent().getBooleanExtra("showCollection", false)){
			collectionBtn.setVisibility(View.VISIBLE);
			collectionView.setVisibility(View.VISIBLE);
		} else{
			collectionBtn.setVisibility(View.GONE);
			collectionView.setVisibility(View.GONE);
		}

		selectDialog = new SelectDialog(mContext);
		selectDialog.addItem("是");
		selectDialog.addItem("否");
		selectDialog.setTag(0);
		selectDialog.setOnDialogItemClickListener(new SelectDialog.OnSelectDialogItemClickListener() {
			@Override
			public void onSelectItemClick(int position) {
				switch ((int)selectDialog.getTag()){
					case 0:
						itemChoiceDesc.setText(position == 0 ? "是" : "否");
						itemChoiceDesc.setTag(position == 0);
						break;
					case 1:
						itemOpenDesc.setText(position == 0 ? "是" : "否");
						itemOpenDesc.setTag(position == 0);
						break;
					case 2:
					default:
						itemHeadlineDesc.setText(position == 0 ? "是" : "否");
						itemHeadlineDesc.setTag(position == 0);
				}
			}
		});
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if(resultCode != RESULT_OK || data == null)
			return;
		if(requestCode == UserSettingEditActivity.REQUEST_CODE){
			String resultData = data.getStringExtra(UserSettingEditActivity.RESULT_DATA);
			switch (data.getIntExtra(UserSettingEditActivity.VIEW_ID, 0x00)) {
				case R.id.item_title:
					itemTitleDesc.setText(resultData);
					break;
				case R.id.item_time:
					itemTimeDesc.setText(resultData);
					break;
				case R.id.item_website:
					itemWebsiteDesc.setText(resultData);
					break;
			}
		} else if(requestCode == SELECT_SORT_TYPE){
			navtype = data.getStringExtra("navtype");
			navtype2 = data.getStringExtra("navtype2");
			itemTypeDesc.setText(String.format("%s(%s)",
					data.getStringExtra("mainName"),
					data.getStringExtra("subName")));
		}
	}

	private void initTitleBar(){
		topName.setText("校正管理");
		leftIcon.setImageResource(R.mipmap.guanzhufanhui);
		rightIcon.setVisibility(View.GONE);
		rightBtn.setVisibility(View.VISIBLE);
		collectionBtn.setVisibility(View.VISIBLE);
		rightBtn.setText("保存");
		collectionBtn.setText("采集");
		leftIcon.setOnClickListener(this);
		rightBtn.setOnClickListener(this);
		collectionBtn.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()){
			case R.id.icon_left:
				onBackPressed();
				break;
			case R.id.right_bn:
				saveChange();
				break;
			case R.id.right_bn1:
				collection();
				break;
			case R.id.item_type:
				startActivityForResult(new Intent(mContext, AdjustSortTypeActivity.class), SELECT_SORT_TYPE);
				break;
			case R.id.item_title:
				UserSettingEditActivity.gotoEdit(this, R.id.item_title,
						"修改标题", (String) itemTitleDesc.getText(), "标题");
				break;
			case R.id.item_choice:
				selectDialog.setTag(0);
				selectDialog.setTitle("是否精选");
				selectDialog.show();
				break;
			case R.id.item_open:
				selectDialog.setTag(1);
				selectDialog.setTitle("是否启用");
				selectDialog.show();
				break;
			case R.id.item_time:
				UserSettingEditActivity.gotoEdit(this, R.id.item_time,
						"修改创建时间", (String) itemTimeDesc.getText(),
						"格式：20170602123042", UserSettingEditActivity.TYPE_DATE);
				break;
			case R.id.item_website:
				UserSettingEditActivity.gotoEdit(this, R.id.item_website,
						"设置网址", (String) itemWebsiteDesc.getText(),
						"网址", UserSettingEditActivity.TYPE_WEB);
				break;
			case R.id.item_headline:
				selectDialog.setTag(2);
				selectDialog.setTitle("是否采集头条号");
				selectDialog.show();
				break;
		}
	}

	protected void saveChange(){
		JsonObject param = new JsonObject();
		param.addProperty("infoId", id);
		param.addProperty("navtype", navtype);
		param.addProperty("navtype2", navtype2);
		param.addProperty("choice", ((boolean)itemChoiceDesc.getTag()) ? 1 : 0);
		param.addProperty("state", ((boolean)itemOpenDesc.getTag()) ? 1 : 0);
		String title = (String) itemTitleDesc.getText();
		if(!TextUtils.isEmpty(title))
			param.addProperty("title", (String) itemTitleDesc.getText());
		try {
			String time = (String) itemTimeDesc.getText();
			param.addProperty("time", dateFormat.parse(time).getTime());
		}catch (Exception e){} finally {
			HttpHelper.getInstance(mContext).headlinesUpdateByAdmin(param, UPDATE_BY_ADMIN_CODE, this);
		}
	}

	private void collection(){
		if(itemWebsiteDesc.getText().toString().trim().length() == 0){
			ToasUtil.toast(mContext, "请输入采集网址");
		} else if(TextUtils.isEmpty(navtype) || TextUtils.isEmpty(navtype2)){
			ToasUtil.toast(mContext, "请选择所属分类");
		} else {
			JsonObject param = new JsonObject();
			param.addProperty("url", itemWebsiteDesc.getText().toString());
			param.addProperty("navtype", navtype);
			param.addProperty("navtype2", navtype2);
			param.addProperty("selected", ((boolean) itemChoiceDesc.getTag()) ? 0 : 1);
			param.addProperty("channel", ((boolean) itemHeadlineDesc.getTag()) ? 0 : 1);
			HttpHelper.getInstance(mContext).spider(param, SPIDER_CODE, this);
		}
	}

	@Override
	public void onStart(int requestType) {
		loading.show();
	}

	@Override
	public void onCompleted(int requestType) {
		loading.cancel();
	}

	@Override
	public void onError(int requestType) {
		onCompleted(requestType);
	}

	@Override
	public void onNext(IModel data, int requestType) {
		if(data.getErrcode() != 0){
			ToasUtil.toast(mContext, data.getMessage());
			onError(requestType);
		} else if(requestType == UPDATE_BY_ADMIN_CODE){
			onBackPressed();
		} else if(requestType == SPIDER_CODE){

		}
	}
}
